<template>
  <div class="comment-container">
    <!-- 搜索框 -->
    <div class="search-container">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入搜索关键词（用户名、留言内容等）"
        @keyup.enter="filterComments"
      ></el-input>
      <el-button size="mini" @click="filterComments">搜索</el-button>
    </div>
    <!-- 评论列表 -->
    <el-row class="mt-1" v-for="(comm, index) in paginatedComments" :key="index">
      <el-col :span="16" :offset="4">
        <el-card class="comment-card">
          <div slot="header" class="comment-header clearfix">
            <span class="comment-id">#{{comm.comment.id}}</span>
            <span class="comment-author">From {{comm.user.userName}} (userid: {{comm.user.id}})</span>
            <span class="comment-contact">Phone: {{comm.user.phone}} Email: {{comm.user.email}}</span>
          </div>
          <div class="comment-content">
            <p class="comment-text">留言内容： {{comm.comment.information}}</p>
            <p class="comment-rating">评分等级： {{comm.comment.type}}星</p>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="filteredComments.length"
    ></el-pagination>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        comment: [],
        searchKeyword: '',
        filteredComments: [],
        currentPage: 1,
        pageSize: 10
      };
    },
    computed: {
      paginatedComments() {
        const start = (this.currentPage - 1) * this.pageSize;
        const end = start + this.pageSize;
        return this.filteredComments.slice(start, end);
      }
    },
    methods: {
      filterComments() {
        this.filteredComments = this.comment.filter(comm => {
          const userName = comm.user.userName.toLowerCase();
          const information = comm.comment.information.toLowerCase();
          const keyword = this.searchKeyword.toLowerCase();
          return userName.includes(keyword) || information.includes(keyword);
        });
        this.currentPage = 1;
      },
      handleSizeChange(newSize) {
        this.pageSize = newSize;
      },
      handleCurrentChange(newPage) {
        this.currentPage = newPage;
      }
    },
    mounted() {
      this.axios.get("http://localhost:9151/admin/listComment")
        .then(res => {
          this.comment = res.data.data;
          this.filteredComments = this.comment;
          console.log(this.comment);
        })
        .catch(res => {
          console.log("res: " + res);
        });
    }
  };
</script>

<style scoped>
  .comment-container {
    padding: 20px;
  }

  .search-container {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
  }

  .comment-card {
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    background-color: #fff;
    margin-bottom: 20px;
    transition: box-shadow 0.3s ease;
  }

  .comment-card:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  }

  .comment-header {
    border-bottom: 1px solid #e0e0e0;
    padding-bottom: 10px;
    margin-bottom: 10px;
  }

  .comment-id {
    font-weight: bold;
    color: #333;
    margin-right: 10px;
  }

  .comment-author {
    color: #666;
    margin-right: 10px;
  }

  .comment-contact {
    color: #999;
  }

  .comment-content {
    padding: 0 10px;
  }

  .comment-text {
    line-height: 1.6;
    color: #444;
  }

  .comment-rating {
    color: #f9a825; /* 金色，用于表示评分 */
    margin-top: 5px;
  }
</style>
